package com.sdk.es.sdk.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author limaosheng
 * @create 2023-08-15 16:45
 * @desc
 */
@Getter
@AllArgsConstructor
public enum EsQueryKeyEnum {
    /**
     * es查询方式
     * 1、filter：对应mysql的 and，与must一样，结果需要算分就使用must，否则可以考虑使用filter，使查询更高效
     * 2、mustNot：对应mysql的 and not
     * 3、should：对应mysql的 or
     * 4、term：对应mysql的 =
     * 5、terms：对应mysql的 in
     * 6、wildcard：对应mysql的 like，入参前后需要拼接*
     * 7、range：范围查询，使用from()、to()
     * 8、exists：对应mysql的 key is not null
     * 9、prefix：对应mysql的 like,前缀匹配
     * 注：因为QueryBuilders只提供了exists方法，所以要查询某个字段为null的记录，得通过mustNot方法 + exists方法组合得方式查询
     */
    FILTER_TERM("filterTerm","and key = param"),
    FILTER_TERMS("filterTerms","and key in (param1,param2...)"),
    FILTER_RANGE("filterRange","and key >= param1 and key <= param2"),
    FILTER_WILDCARD("filterWildcard","and key like '%param%'"),
    FILTER_EXISTS("filterExists","and key is not null"),
    FILTER_PREFIX("filterPrefix","and key like 'param%'"),
    FILTER_LT("filterLt","and key < param2"),
    FILTER_GT("filterGt","and key > param2"),
    /**
     * not
     */
    MUST_NOT_EXISTS("mustNotExists","and key is null"),
    MUST_NOT_TERM("mustNotTerm","and key != param"),
    MUST_NOT_TERMS("mustNotTerms","and key not in (param1,param2...)"),
    /**
     * 以下关键字为直接拼接在条件后，类比mysql：where key1 = param1 or key2 = param2 or key3 = param3
     */
    SHOULD_TERM("shouldTerm","or key = param"),
    SHOULD_TERMS("shouldTerms","or key in (param,param...)"),
    SHOULD_EXISTS("shouldExists","or key is not null"),
    SHOULD_MUST_NOT_EXISTS("shouldMustNotExists","or key is null"),
    SHOULD_WILDCARD("shouldWildcard","or key like '%param%'"),
    /**
     * 以下关键字合并所有should查询后再拼接一个filter，类比mysql：where key1 = param1 and (key2 = param2 or key3 = param3)
     */
    FILTER_SHOULD_TERM("filterShouldTerm","and ( ? or key = param)"),
    FILTER_SHOULD_TERMS("filterShouldTerms","and ( ? or key in (param,param...))"),
    FILTER_SHOULD_EXISTS("filterShouldExists","and ( ? or key is not null)"),
    FILTER_SHOULD_MUST_NOT_EXISTS("filterShouldMustNotExists","and ( ? or key is null)"),
    FILTER_SHOULD_WILDCARD("filterShouldWildcard","and ( ? or key like '%param%')");

    private String key;
    private String sql;

    /**
     * 所有查询方式
     * @return
     */
    public static List<String> keyList() {
        List<String> list = new ArrayList<>();
        for (EsQueryKeyEnum e : EsQueryKeyEnum.values()) {
            list.add(e.getKey());
        }
        return list;
    }

    /**
     * 需要传入单个参数的查询方式
     * @return
     */
    public static List<String> param() {
        List<String> list = new ArrayList<>();
        list.add(EsQueryKeyEnum.FILTER_TERM.getKey());
        list.add(EsQueryKeyEnum.FILTER_WILDCARD.getKey());
        list.add(EsQueryKeyEnum.MUST_NOT_TERM.getKey());
        list.add(EsQueryKeyEnum.SHOULD_TERM.getKey());
        list.add(EsQueryKeyEnum.SHOULD_WILDCARD.getKey());
        list.add(EsQueryKeyEnum.FILTER_SHOULD_TERM.getKey());
        list.add(EsQueryKeyEnum.FILTER_SHOULD_WILDCARD.getKey());
        list.add(EsQueryKeyEnum.FILTER_PREFIX.getKey());
        list.add(EsQueryKeyEnum.FILTER_LT.getKey());
        list.add(EsQueryKeyEnum.FILTER_GT.getKey());
        return list;
    }

    /**
     * 入参为list的查询方式
     * @return
     */
    public static List<String> paramList() {
        List<String> list = new ArrayList<>();
        list.add(EsQueryKeyEnum.FILTER_TERMS.getKey());
        list.add(EsQueryKeyEnum.MUST_NOT_TERMS.getKey());
        list.add(EsQueryKeyEnum.SHOULD_TERMS.getKey());
        list.add(EsQueryKeyEnum.FILTER_SHOULD_TERMS.getKey());
        return list;
    }
}
